package com.cogent.dynamicform.form.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.business.helper.HtmlUIHelper;
import com.cogent.common.util.MapUtil;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.type.DistrictControl;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;
import com.opensymphony.oscache.util.StringUtil;

public class LevelSelectDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		String[] typecodes = formColumnDomain.getTypecode().split("\\|");
		String sql = typecodes[0];
		String orderby = typecodes[1];
		List queryList = new ArrayList();
		HashMap curQueryMap = new HashMap();
		if(typecodes.length > 2) {
			String[] param = typecodes[2].split(";");
			for(int i=0; i<param.length; i++) {
				String[] curtemps = param[i].split("~");
				String value = MapUtil.getStringValue(formMap, curtemps[0]);
				if(curtemps[0].equals("districtcode"))
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], DistrictControl.getControlCode(value), DistrictControl.getQueryType(value)));
				else
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], value));
				curQueryMap.put(curtemps[0], value);
			}
		}
		String defaultValue = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		int keyindex = 0;
		if(typecodes.length > 3) {
			String[] curtemp = typecodes[3].split("~");
			for(int i=0; i<curtemp.length; i++) {
				if(curtemp[i].equals(formColumnDomain.getName()))
					keyindex = i;
			}
		}
		List list = BussinessQueryHelper.query(sql, queryList, orderby);
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(formColumnDomain.getName()).append("\" id=\"").append(formColumnDomain.getName()).append("\"/>");
		int level = 1;
		str.append(HtmlUIHelper.getOption("--请选择","", defaultValue, level));
		if(list.size() > 0) {
			HashMap curMap = (HashMap)list.get(0);
			level = MapUtil.getIntValue(curMap, "organlevel", 1) - 1;
		}
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap dataMap = (HashMap)it.next();
			String[] options = MapUtil.getStringValue(dataMap, "enumvalue").split("~");
			str.append(HtmlUIHelper.getOption(MapUtil.getStringValue(dataMap, "enumoption"), options[keyindex], defaultValue,
						MapUtil.getIntValue(dataMap, "enumlevel") - level));
		}
		str.append("</select>");
		
		return str.toString();
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap) + "&nbsp;";
		String[] typecodes = formColumnDomain.getTypecode().split("\\|");
		String sql = typecodes[0];
		String orderby = typecodes[1];
		List queryList = new ArrayList();
		HashMap curQueryMap = new HashMap();
		if(typecodes.length > 2) {
			String[] param = typecodes[2].split(";");
			for(int i=0; i<param.length; i++) {
				String[] curtemps = param[i].split("~");
				String value = MapUtil.getStringValue(formMap, curtemps[0]);
				if(curtemps[0].equals("districtcode"))
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], DistrictControl.getControlCode(value), DistrictControl.getQueryType(value)));
				else
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], value));
				curQueryMap.put(curtemps[0], value);
			}
		}
		String defaultValue = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		if(typecodes.length > 3) {
			String[] curtemp = typecodes[3].split("~");
			defaultValue = "";
			for(int i=0; i<curtemp.length; i++)
				defaultValue += MapUtil.getStringValue(formMap, curtemp[i]) + "~";
			defaultValue = defaultValue.substring(0, defaultValue.length()-1);
		}
		List list = BussinessQueryHelper.query(sql, queryList, orderby);
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(formColumnDomain.getName() + "_1").append("\" id=\"").append(formColumnDomain.getName() + "_1").append("\" ")
			.append(formColumnDomain.getJs()).append("/>");
		int level = 1;
		str.append(HtmlUIHelper.getOption("--请选择","", defaultValue, level));
		if(list.size() > 0) {
			HashMap curMap = (HashMap)list.get(0);
			level = MapUtil.getIntValue(curMap, "organlevel", 1) - 1;
		}
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap dataMap = (HashMap)it.next();
			str.append(HtmlUIHelper.getOption(MapUtil.getStringValue(dataMap, "enumoption"),
						MapUtil.getStringValue(dataMap, "enumvalue"), defaultValue,
						MapUtil.getIntValue(dataMap, "enumlevel") - level));
		}
		str.append("</select>");
		if(formColumnDomain.isIsrequired())
			str.append("<span class=\"txt_red\">&nbsp;*</span>");
		str.append("<input type=\"hidden\" name=\"").append(formColumnDomain.getName()).append("\" id=\"").append(formColumnDomain.getName())
			.append("\" value=\"").append(MapUtil.getStringValue(formMap, formColumnDomain.getName()))
			.append("\"/>");
		return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		String[] typecodes = formColumnDomain.getTypecode().split("\\|");
		String sql = typecodes[0];
		String orderby = typecodes[1];
		List queryList = new ArrayList();
		HashMap curQueryMap = new HashMap();
		if(typecodes.length > 2) {
			String[] param = typecodes[2].split(";");
			for(int i=0; i<param.length; i++) {
				String[] curtemps = param[i].split("~");
				String value = MapUtil.getStringValue(formMap, curtemps[0]);
				if(curtemps[0].equals("districtcode"))
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], DistrictControl.getControlCode(value), DistrictControl.getQueryType(value)));
				else
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], value));
				curQueryMap.put(curtemps[0], value);
			}
		}
		String defaultValue = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		int keyindex = 0;
		if(typecodes.length > 3) {
			String[] curtemp = typecodes[3].split("~");
			for(int i=0; i<curtemp.length; i++) {
				if(curtemp[i].equals(formColumnDomain.getName()))
					keyindex = i;
			}
		}
		List list = BussinessQueryHelper.query(sql, queryList, orderby);
		String ret = "";
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap dataMap = (HashMap)it.next();
			String[] enumvalue = MapUtil.getStringValue(dataMap, "enumvalue").split("~");
			if(enumvalue[keyindex].equals(defaultValue)) {
				ret = MapUtil.getStringValue(dataMap, "enumoption");
				break;
			}
		}
		if(StringUtil.isEmpty(ret))
			ret = "&nbsp;";
		return ret;
	}
}
